package com.icesoft.system.safe.filter.impl;

import com.icesoft.framework.core.util.ResponseUtils;
import com.icesoft.framework.core.vo.JsonResult;
import com.icesoft.system.safe.filter.HeadSafeRequestFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class RequiredHeadFilter implements HeadSafeRequestFilter {

	@Override
	public boolean headFilter(HttpServletRequest request, HttpServletResponse response) {
		String method = request.getMethod();
		log.trace("http method:{}", method);
		if (request.getHeader("versionCode") == null) {
			ResponseUtils.writeJson(response, JsonResult.FAIL_EXCEPTION("缺少请求头：versionCode"));
			return false;
		}
		if (request.getHeader("clientType") == null) {
			ResponseUtils.writeJson(response, JsonResult.FAIL_EXCEPTION("缺少请求头：clientType"));
			return false;
		}
		return true;
	}

}
